package com.tddx.activiti.business.sys.util;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Component;

import com.tddx.activiti.business.sys.entity.systuser.SysTuserEntity;
import com.tddx.activiti.common.Message;

@Component
public class LoginUtil {
	
	private static Logger log = Logger.getLogger(LoginUtil.class);
	
	public static Subject getSubject(){
		return SecurityUtils.getSubject();
	}
	
	public static String login(UsernamePasswordToken token){
		try {
			getSubject().login(token);
			log.info("登陆成功");
			return Message.SUCCESS;
		} catch (Exception e) {
			log.error("登陆失败：" + e);
			return Message.FAILED;
		}
	}
	
	public static String login(String vcUserName, String vcPass){
		UsernamePasswordToken token = new UsernamePasswordToken(vcUserName, vcPass);
		return login(token);
	}
	
	public static String login(SysTuserEntity entity){
		return login(entity.getVcUsername(), entity.getVcPass());
	}
	
	public static void logout(){
		getSubject().logout();
	}
}
